import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';

class InputMinField extends StatefulWidget {
  final String tip;
  final TextEditingController controller;
  final FocusNode focusNode;
  final String helpText;
  final bool hasError;
  final TextInputType keyboardType;
  final List<TextInputFormatter> inputFormatters;
  final double width;

  const InputMinField({
    Key key,
    this.helpText,
    this.width,
    this.keyboardType,
    this.inputFormatters,
    this.hasError = false,
    this.tip,
    this.controller,
    this.focusNode,
  }) : super(key: key);

  @override
  _InputMinFieldState createState() => _InputMinFieldState();
}

class _InputMinFieldState extends State<InputMinField> {
  bool _hasFocus = false;
  bool obscureText = false;

  @override
  void initState() {
    super.initState();
    widget.focusNode?.addListener(() {
      if (widget.focusNode.hasFocus) {
        if (_hasFocus == false) {
          setState(() {
            _hasFocus = true;
          });
        }
      } else {
        if (_hasFocus == true) {
          setState(() {
            _hasFocus = false;
          });
        }
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
            width: widget.width ?? getDp(77),
            height: getDp(54),
            padding: EdgeInsets.symmetric(
              horizontal: getDp(12),
            ),
            decoration: BoxDecoration(
              color: Colors.white,
              border: _hasFocus == true
                  ? Border.all(
                      color: aceBlueColor,
                      width: getDp(1),
                    )
                  : Border(),
              boxShadow: [
                BoxShadow(
                  color: Colors.black.withOpacity(0.25),
                  blurRadius: getDp(22),
                  spreadRadius: -getDp(5),
                ),
              ],
              borderRadius: BorderRadius.circular(getDp(15)),
            ),
            child: TextField(
              inputFormatters: widget.inputFormatters,
              controller: widget.controller,
              focusNode: widget.focusNode,
              scrollPadding: EdgeInsets.zero,
              obscureText: obscureText,
              keyboardType: widget.keyboardType,
              style: TextStyle(
                fontSize: getSp(16),
                color: aceBlackColor,
                fontWeight: FontWeight.bold,
              ),
              decoration: InputDecoration(
                  border: InputBorder.none,
                  focusedBorder: InputBorder.none,
                  disabledBorder: InputBorder.none,
                  labelText: widget.tip,
                  labelStyle: TextStyle(
                      color: widget.hasError ? aceRedColor : aceDarkGrayColor,
                      fontWeight: FontWeight.w700,
                      fontSize: getSp(16),
                      height: 16 / 16)),
            )),
        if (widget.helpText != null)
          Padding(
            padding: EdgeInsets.only(
              left: getDp(59),
              top: getDp(5),
            ),
            child: Text(
              widget.helpText,
              style: TextStyle(
                color: widget.hasError ? aceRedColor : aceDarkGrayColor,
                fontSize: getSp(12),
                fontWeight: FontWeight.bold,
                height: 16 / 12,
              ),
            ),
          ),
      ],
    );
  }

  void onChangeObscureText() {
    setState(() {
      obscureText = !obscureText;
    });
  }
}
